/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.IllegalOrphanException;
import entityprojet.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author T.Ron
 */
public class PostebudgetaireJpaController implements Serializable {

    public PostebudgetaireJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Postebudgetaire postebudgetaire) {
        if (postebudgetaire.getBudgetpbCollection() == null) {
            postebudgetaire.setBudgetpbCollection(new ArrayList<Budgetpb>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Sectionanal sa = postebudgetaire.getSa();
            if (sa != null) {
                sa = em.getReference(sa.getClass(), sa.getIdSa());
                postebudgetaire.setSa(sa);
            }
            Collection<Budgetpb> attachedBudgetpbCollection = new ArrayList<Budgetpb>();
            for (Budgetpb budgetpbCollectionBudgetpbToAttach : postebudgetaire.getBudgetpbCollection()) {
                budgetpbCollectionBudgetpbToAttach = em.getReference(budgetpbCollectionBudgetpbToAttach.getClass(), budgetpbCollectionBudgetpbToAttach.getBudgetpbPK());
                attachedBudgetpbCollection.add(budgetpbCollectionBudgetpbToAttach);
            }
            postebudgetaire.setBudgetpbCollection(attachedBudgetpbCollection);
            em.persist(postebudgetaire);
            if (sa != null) {
                sa.getPostebudgetaireCollection().add(postebudgetaire);
                sa = em.merge(sa);
            }
            for (Budgetpb budgetpbCollectionBudgetpb : postebudgetaire.getBudgetpbCollection()) {
                Postebudgetaire oldPostebudgetaireOfBudgetpbCollectionBudgetpb = budgetpbCollectionBudgetpb.getPostebudgetaire();
                budgetpbCollectionBudgetpb.setPostebudgetaire(postebudgetaire);
                budgetpbCollectionBudgetpb = em.merge(budgetpbCollectionBudgetpb);
                if (oldPostebudgetaireOfBudgetpbCollectionBudgetpb != null) {
                    oldPostebudgetaireOfBudgetpbCollectionBudgetpb.getBudgetpbCollection().remove(budgetpbCollectionBudgetpb);
                    oldPostebudgetaireOfBudgetpbCollectionBudgetpb = em.merge(oldPostebudgetaireOfBudgetpbCollectionBudgetpb);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Postebudgetaire postebudgetaire) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Postebudgetaire persistentPostebudgetaire = em.find(Postebudgetaire.class, postebudgetaire.getIdPb());
            Sectionanal saOld = persistentPostebudgetaire.getSa();
            Sectionanal saNew = postebudgetaire.getSa();
            Collection<Budgetpb> budgetpbCollectionOld = persistentPostebudgetaire.getBudgetpbCollection();
            Collection<Budgetpb> budgetpbCollectionNew = postebudgetaire.getBudgetpbCollection();
            List<String> illegalOrphanMessages = null;
            for (Budgetpb budgetpbCollectionOldBudgetpb : budgetpbCollectionOld) {
                if (!budgetpbCollectionNew.contains(budgetpbCollectionOldBudgetpb)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Budgetpb " + budgetpbCollectionOldBudgetpb + " since its postebudgetaire field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (saNew != null) {
                saNew = em.getReference(saNew.getClass(), saNew.getIdSa());
                postebudgetaire.setSa(saNew);
            }
            Collection<Budgetpb> attachedBudgetpbCollectionNew = new ArrayList<Budgetpb>();
            for (Budgetpb budgetpbCollectionNewBudgetpbToAttach : budgetpbCollectionNew) {
                budgetpbCollectionNewBudgetpbToAttach = em.getReference(budgetpbCollectionNewBudgetpbToAttach.getClass(), budgetpbCollectionNewBudgetpbToAttach.getBudgetpbPK());
                attachedBudgetpbCollectionNew.add(budgetpbCollectionNewBudgetpbToAttach);
            }
            budgetpbCollectionNew = attachedBudgetpbCollectionNew;
            postebudgetaire.setBudgetpbCollection(budgetpbCollectionNew);
            postebudgetaire = em.merge(postebudgetaire);
            if (saOld != null && !saOld.equals(saNew)) {
                saOld.getPostebudgetaireCollection().remove(postebudgetaire);
                saOld = em.merge(saOld);
            }
            if (saNew != null && !saNew.equals(saOld)) {
                saNew.getPostebudgetaireCollection().add(postebudgetaire);
                saNew = em.merge(saNew);
            }
            for (Budgetpb budgetpbCollectionNewBudgetpb : budgetpbCollectionNew) {
                if (!budgetpbCollectionOld.contains(budgetpbCollectionNewBudgetpb)) {
                    Postebudgetaire oldPostebudgetaireOfBudgetpbCollectionNewBudgetpb = budgetpbCollectionNewBudgetpb.getPostebudgetaire();
                    budgetpbCollectionNewBudgetpb.setPostebudgetaire(postebudgetaire);
                    budgetpbCollectionNewBudgetpb = em.merge(budgetpbCollectionNewBudgetpb);
                    if (oldPostebudgetaireOfBudgetpbCollectionNewBudgetpb != null && !oldPostebudgetaireOfBudgetpbCollectionNewBudgetpb.equals(postebudgetaire)) {
                        oldPostebudgetaireOfBudgetpbCollectionNewBudgetpb.getBudgetpbCollection().remove(budgetpbCollectionNewBudgetpb);
                        oldPostebudgetaireOfBudgetpbCollectionNewBudgetpb = em.merge(oldPostebudgetaireOfBudgetpbCollectionNewBudgetpb);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = postebudgetaire.getIdPb();
                if (findPostebudgetaire(id) == null) {
                    throw new NonexistentEntityException("The postebudgetaire with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Postebudgetaire postebudgetaire;
            try {
                postebudgetaire = em.getReference(Postebudgetaire.class, id);
                postebudgetaire.getIdPb();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The postebudgetaire with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Budgetpb> budgetpbCollectionOrphanCheck = postebudgetaire.getBudgetpbCollection();
            for (Budgetpb budgetpbCollectionOrphanCheckBudgetpb : budgetpbCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Postebudgetaire (" + postebudgetaire + ") cannot be destroyed since the Budgetpb " + budgetpbCollectionOrphanCheckBudgetpb + " in its budgetpbCollection field has a non-nullable postebudgetaire field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Sectionanal sa = postebudgetaire.getSa();
            if (sa != null) {
                sa.getPostebudgetaireCollection().remove(postebudgetaire);
                sa = em.merge(sa);
            }
            em.remove(postebudgetaire);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Postebudgetaire> findPostebudgetaireEntities() {
        return findPostebudgetaireEntities(true, -1, -1);
    }

    public List<Postebudgetaire> findPostebudgetaireEntities(int maxResults, int firstResult) {
        return findPostebudgetaireEntities(false, maxResults, firstResult);
    }

    private List<Postebudgetaire> findPostebudgetaireEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Postebudgetaire.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Postebudgetaire findPostebudgetaire(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Postebudgetaire.class, id);
        } finally {
            em.close();
        }
    }

    public int getPostebudgetaireCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Postebudgetaire> rt = cq.from(Postebudgetaire.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
